package com.jams.goodox.activity.serve.enterprise;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.entity.resp.EnterpriseMentorListResp;
import com.jams.goodox.entity.resp.EnterpriseMentorListResp.DataBean.ResultListBean;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;
import com.jams.library.widget.RecyclerDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * 企业导师
 */
public class EnterpriseMentorActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @Bind(R.id.empty_view_mentor)
    LoadView emptyView;
    @Bind(R.id.refresh_enterprise_mentor)
    RefreshRecyclerView recycleEnterpriseMentor;
    @Bind(R.id.srf_enterprise_mentor)
    SwipeRefreshLayout refreshLayout;

    RecycleCommonAdapter<ResultListBean> mAdapter;
    List<ResultListBean> dataList = new ArrayList<>();
    private int curPage;
    public final static String MENTOR = "MENTOR";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_enterprise_mentor;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, getString(R.string.enterprise_mentor));
        setData();
    }

    /**
     * 设置数据
     */
    private void setData() {
        refreshLayout.setOnRefreshListener(this);
        recycleEnterpriseMentor.setLayoutManager(new RefreshLinearLayoutManager(this));
        recycleEnterpriseMentor.setHasFixedSize(true);
        recycleEnterpriseMentor.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new RecycleCommonAdapter<ResultListBean>(mContext, dataList, R.layout.item_enterprise_mentor) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final ResultListBean item, int position) {
                helper.setSimpleDraweeView(R.id.sdv_head_mentor, StringUtils.newString(item.photo))
                        .setText(R.id.tv_name, item.name.trim())
                        .setText(R.id.tv_job_tittle, item.position.trim())
                        .setText(R.id.tv_company_name, item.company.trim());
                helper.setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        Intent i = new Intent(mContext, EnterpriseMentorIntroduceActivity.class);
                        i.putExtra(MENTOR, StringUtils.newString(item.id));
                        startActivity(i);
                    }
                });
            }
        };
        //增加headView
        View v = new View(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dp2px(mContext, 8));
        v.setLayoutParams(params);
        mAdapter.setHeaderView(v);
        recycleEnterpriseMentor.setAdapter(mAdapter);

        recycleEnterpriseMentor.addItemDecoration(new RecyclerDivider(//分割线 1px
                mContext,
                LinearLayoutManager.VERTICAL,
                1,
                ContextCompat.getColor(getContext(),
                        R.color.shallow_divider)));
        recycleEnterpriseMentor.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                getData();
            }
        });
        onRefresh();
    }


    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                try {
                    refreshLayout.setRefreshing(true);
                    curPage = 1;
                    getData();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * 获取企业导师列表
     */
    private void getData() {
        enqueue(createCallService(EnterpriseService.class).getMentorList(curPage, AppConfig.PagSize), new RetrofitCallbackListener<EnterpriseMentorListResp>() {
            @Override
            public void onSuccess(Call<EnterpriseMentorListResp> call, EnterpriseMentorListResp result) {
                try {
                    refreshLayout.setRefreshing(false);
                    recycleEnterpriseMentor.loadMoreComplete();
                    if (result.data != null) {
                        if (curPage == 1) {
                            dataList.clear();
                        }
                        if (result.data.pageInfo == null) {
                            recycleEnterpriseMentor.setCanLoadMore(false);
                        } else {
                            recycleEnterpriseMentor.setCanLoadMore(result.data.pageInfo.hasMore);
                        }
                        dataList.addAll(result.data.resultList);
                        curPage++;
                        recycleEnterpriseMentor.notifyData();
                    }
                    if (!CollectionUtils.hasData(dataList)) {
                        emptyView.loadCompleteNoData(R.mipmap.holder_no_enterprise_mentor,R.string.no_mentor);
                    } else {
                        emptyView.loadComplete();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(String msg) {
                try {
                    refreshLayout.setRefreshing(false);
                    recycleEnterpriseMentor.loadMoreComplete();
                    if (!CollectionUtils.hasData(dataList)) {
                        emptyView.loadCompleteNoData(R.mipmap.holder_no_enterprise_mentor,R.string.no_mentor);
                    } else {
                        emptyView.loadComplete();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }
}
